package com.example.wanbaqian.activity;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.wanbaqian.R;
import com.example.wanbaqian.base.BaseActivity;
import com.example.wanbaqian.config.CommonValue;
import com.example.wanbaqian.base.HttpCallback;
import com.example.wanbaqian.config.HttpURL;
import com.example.wanbaqian.bean.Info;
import com.example.wanbaqian.bean.UserInfoBean;
import com.example.wanbaqian.bean.WalletFragmentMoeyBean;
import com.example.wanbaqian.bean.WithdrawalsBean;
import com.example.wanbaqian.utils.GetJsonUtils;
import com.example.wanbaqian.utils.HttpUtils;
import com.example.wanbaqian.utils.JsonUtils;
import com.example.wanbaqian.utils.SPUtils;
import com.example.wanbaqian.widget.BubbleSeekBar;

import java.math.BigDecimal;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 提现界面
 */
public class WithdrawalsActivity extends BaseActivity implements HttpCallback {
    private static final String TAG = "WithdrawalsActivity";
    @BindView(R.id.Withdrawals_sumMoney)
    TextView WithdrawalsSumMoney;
    @BindView(R.id.Withdrawals_sumMoneyBank)
    TextView WithdrawalsSumMoneyBank;
    @BindView(R.id.Withdrawals_moneySeekBar)
    BubbleSeekBar WithdrawalsMoneySeekBar;
    @BindView(R.id.Withdrawals_moneyMin)
    TextView WithdrawalsMoneyMin;
    @BindView(R.id.Withdrawals_moneyMax)
    TextView WithdrawalsMoneyMax;
    @BindView(R.id.Withdrawals_timeSeekBar)
    BubbleSeekBar WithdrawalsTimeSeekBar;
    @BindView(R.id.Withdrawals_timeMin)
    TextView WithdrawalsTimeMin;
    @BindView(R.id.Withdrawals_timeMax)
    TextView WithdrawalsTimeMax;
    @BindView(R.id.Withdrawals_moneyTv)
    TextView WithdrawalsMoneyTv;
    @BindView(R.id.Withdrawals_timeTv)
    TextView WithdrawalsTimeTv;
    @BindView(R.id.Withdrawals_ok)
    Button WithdrawalsOk;
    private WithdrawalsBean bean;
    private int minMoney;
    public static Activity instence;


    @Override
    protected int getLayout() {
        return R.layout.activity_withdrawals;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        instence = this;
        setStatusBarColor(R.color.colorBackground);
        setToolBar("借贷");
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        WithdrawalsMoneySeekBar.setOnProgressChangedListener(new BubbleSeekBar.OnProgressChangedListener() {
            @Override
            public void onProgressChanged(int progress, float progressFloat) {
                WithdrawalsMoneyTv.setText(progress + "元");
                bean.setMoney(progress + "");
            }

            @Override
            public void getProgressOnActionUp(int progress, float progressFloat) {

            }

            @Override
            public void getProgressOnFinally(int progress, float progressFloat) {

            }
        });
        WithdrawalsTimeSeekBar.setOnProgressChangedListener(new BubbleSeekBar.OnProgressChangedListener() {
            @Override
            public void onProgressChanged(int progress, float progressFloat) {
                bean.setTime(progress + "");
                WithdrawalsTimeTv.setText(progress + "天");
            }

            @Override
            public void getProgressOnActionUp(int progress, float progressFloat) {

            }

            @Override
            public void getProgressOnFinally(int progress, float progressFloat) {

            }
        });
    }

    @Override
    protected void initDate() {
        bean = (WithdrawalsBean) getIntent().getExtras().getSerializable("bean");
        Log.e(TAG, "initDate: "+bean.toString());
        setDate();
    }

    @OnClick(R.id.Withdrawals_ok)
    public void onClick() {
        if (bean.getMoney().equals("0")) {
            showToast("请选择借款金额");
        } else {
            /*Bundle bundle = new Bundle();
            bundle.putSerializable("bean", bean);
            Log.i(TAG, "onClick: " + bean.toString());
            openActivity(WithdrawalsConfirmActivity.class, bundle);*/
            getZHFY(""+WithdrawalsMoneySeekBar.getProgress(),""+WithdrawalsTimeSeekBar.getProgress());

        }
    }

    /**
     * 初始化数据
     */
    private void setDate() {
        WithdrawalsSumMoney.setText(bean.getLoan_max());
        WithdrawalsSumMoneyBank.setText("收款账号:" + bean.getBank_account() + " " + bean.getBank_name());
        String minMoney2 = bean.getLoan_min();
        String maxMoney2 = bean.getLoan_max();


        double minMoney1 = Double.valueOf(bean.getLoan_min());
        double maxMoney1 = Double.valueOf(bean.getLoan_max());

        minMoney = (int) minMoney1;
        int maxMoney = (int) maxMoney1;

        //判断额度500时候--最小值为0
        if (minMoney >= maxMoney) {
            WithdrawalsMoneyMin.setText(0 + "元");
        } else {
            WithdrawalsMoneyMin.setText(minMoney2 + "元");
        }
        WithdrawalsMoneyMax.setText(maxMoney2 + "元");

        if (minMoney >= maxMoney) {
            minMoney = 0;
        }
        int minDay = Integer.valueOf(bean.getDays_min());
        int maxDay = Integer.valueOf(bean.getDays_max());

        WithdrawalsTimeMin.setText(minDay + "天");
        WithdrawalsTimeMax.setText(maxDay + "天");

        WithdrawalsMoneySeekBar.getConfigBuilder().max(Integer.valueOf(maxMoney))
                .sectionCount((Integer.valueOf(maxMoney) / 500) - 1)
                .min(Integer.valueOf(minMoney)).build();
        WithdrawalsTimeSeekBar.getConfigBuilder().max(maxDay).min(minDay).build();
        WithdrawalsMoneyTv.setText(minMoney + "元");
        WithdrawalsTimeTv.setText(minDay + "天");
    }


    /**
     * 获取综合费用
     */
    private void getZHFY(String money,String days) {
        UserInfoBean userInfo = SPUtils.getUserInfo(this);
        int user_id = 0;
        if(null != userInfo){
            user_id = userInfo.getUser_id();
        }else{
            user_id = -1;
        }
        HttpUtils.getInstance().post(
                GetJsonUtils.getInstance().getRequest(HttpURL.GET_LV, mContext,
                        new String[]
                                {
                                        "accessid"
                                        , "amount"
                                        , "agree"
                                        , "userId"
                                        , "days"
                                },
                        new String[]{
                                SPUtils.getAccessid(mContext)
                                ,
                                money + ""
                                , "agree"
                                , "" + user_id
                                , "" + days
                        })//这个是post第一个字段

                        , mContext
                , null
                , CommonValue.REQUEST_CODE4
                , this);
    }



    @Override
    public void onSuccess(String content, Info info, int requestCode) {
        switch (requestCode) {

            case CommonValue.REQUEST_CODE4:

//                Log.e(TAG, "onSuccess: "+content);
                if (!TextUtils.isEmpty(content)) {

                    Log.e(TAG, "onSuccess: "+content);
                    WalletFragmentMoeyBean moneyBean = JsonUtils.getInstance().jsonToObjectk(content, WalletFragmentMoeyBean.class);
                    if (moneyBean != null) {

                        //先把费率初始化了
                       String interest_rates = moneyBean.getInterest_rate();
//                        Log.e(TAG, "利率String: " + interest_rates);

                        SPUtils.put(this,"lvString",interest_rates);
                        //开始计算综合费用《计算公式是：服务费+借款金额*借款天数*利率》
                        String service_amount_string = moneyBean.getService_amount();//服务费用
//                        Log.e(TAG, "服务费String: " + service_amount_string);
                        SPUtils.put(this,"serviceString",service_amount_string);


                        String loan_amount_string = moneyBean.getLoan_amount();
//                        Log.e(TAG, "借款金额String: " + loan_amount_string);
                        SPUtils.put(this,"amountString",loan_amount_string);


                        int min_days = WithdrawalsTimeSeekBar.getProgress();
//                        Log.e(TAG, "借款天数String: " + min_days);
                        SPUtils.put(this,"dayString",""+min_days);

                        String real_amount = moneyBean.getReal_amount();
                        SPUtils.put(this,"realAmountString",""+real_amount);

                        /*--------------------------------------------------------*/

                        BigDecimal interest_rates_big = new BigDecimal(interest_rates);
//                        Log.e(TAG, "利率BigDecimal: " + interest_rates_big);

                        BigDecimal service_amount_big = new BigDecimal(service_amount_string);
//                        Log.e(TAG, "服务费BigDecimal: " + service_amount_big);

                        BigDecimal loan_amount_big = new BigDecimal(loan_amount_string);
//                        Log.e(TAG, "借款金额BigDecimal: " + loan_amount_big);


                        BigDecimal min_days_big = new BigDecimal(min_days);
//                        Log.e(TAG, "借款天数BigDecimal: " + min_days_big);

                        //先进行计算利息《金额*天数*利率》
                        double v = loan_amount_big
                                .multiply(min_days_big)
                                .multiply(interest_rates_big)
//                                .add(service_amount_big)//不加服务费是单纯利息
                                .doubleValue();//综合费用
                        SPUtils.put(this,"lixiString",v);

                        Bundle bundle = new Bundle();
                        bundle.putSerializable("bean", bean);
                        //2017年9月15日四个协议存储本地
                        String quick_sign = moneyBean.getAgreement();//快速签署协议
                        SPUtils.put(this,"getAgreement",""+quick_sign);

                        openActivity(WithdrawalsConfirmActivity.class, bundle);

                    }
                } else {
                    showToast("系统忙，请重试");
                }

                break;
        }
    }

    @Override
    public void onCacheSuccess(String content, Info info, int requestCode) {

    }

    @Override
    public void onError(Info info, int requestCode) {
        showToast(info.getMsg());
    }
}
